package tw.asap.plus.network.config;




import tw.asap.plus.common.ConstValue;
import tw.asap.plus.network.exception.RequestException;
import retrofit.ErrorHandler;
import retrofit.RetrofitError;

/**
 * 网络层错误拦截
 *
 * Created by LiFZhe on 3/11/15.
 */
public class HErrorHandler implements ErrorHandler {

    @Override
    public Throwable handleError(RetrofitError cause) {
        cause.printStackTrace();
        String errorType = "";
        if (cause.getCause() != null) {
            errorType = cause.getCause().toString();
            System.out.println("errorType------->" + errorType);
        }
        int httpCode = -1;
        if (cause.getResponse() != null) {
            httpCode = cause.getResponse().getStatus();
            System.out.println("cause.getResponse().getStatus()------->" + httpCode);
        }
        String shownMsg = "获取数据失败，请稍后重试";
        switch (cause.getKind()) {
            case HTTP:
                shownMsg = ConstValue.SERVER_ERROR;
                break;
            case NETWORK:
                shownMsg = ConstValue.INTERNET_ERROR;
                break;
            case CONVERSION:
                shownMsg = ConstValue.CONVERSION_ERROR;
                break;
            case UNEXPECTED:
                shownMsg = "未知的网络异常哦";
                break;
        }
        return new RequestException(shownMsg);
    }
}
